import 'package:flutter/material.dart';
import 'package:core_package/core_package.dart';
import 'package:flutter_app_unionad/model/cash_detail_list_item.dart';
import 'package:flutter_app_unionad/request/facility_request.dart';
import 'package:flutter_app_unionad/ui/user_center/widget/withdraw_deposit_order_item_widget.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

//资金明细 提现
class FinancialDetailOneWidget extends StatefulWidget {
  @override
  _FinancialDetailOneWidgetState createState() => _FinancialDetailOneWidgetState();
}

class _FinancialDetailOneWidgetState extends State<FinancialDetailOneWidget> {
  int page = 0;
  EasyRefreshController? _controller;
  List<CashDetailListItem>? dataSource;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _controller = EasyRefreshController();
    getData();
  }

  void getData() {
    page = 1;
    FacilityRequest.cashDetailList(pageNum: page).then((value) {
      if (value != null) {
        if (dataSource == null) dataSource = [];
        dataSource?.clear();
        dataSource?.addAll(value);
      }
      if (mounted) setState(() {});
    });
  }

  void getMoreData() {
    page++;
    FacilityRequest.cashDetailList(pageNum: page).then((value) {
      if (value != null) {
        if (dataSource == null) dataSource = [];
        dataSource?.addAll(value);
      }
      if (dataSource != null && dataSource!.length < 20) {
        _controller!.finishLoadCallBack?.call();
      } else {
        _controller!.resetLoadStateCallBack?.call();
      }
      if (mounted) setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    if (dataSource == null) {
      return ProgressDialog.showFirstLoading();
    }
    if (dataSource!.length == 0) {
      return Center(child: EmptyWidget());
    }
    return EasyRefresh(
      header: RefreshBasics.header,
      footer: RefreshBasics.footer,
      controller: _controller,
      onRefresh: () async {
        getData();
      },
      onLoad: () async {
        getMoreData();
      },
      child: ListView.builder(
        padding: EdgeInsets.zero,
        itemCount: dataSource!.length,
        itemBuilder: (ctx, index) {
          return WithdrawDepositOrderItemWidget(
            item: dataSource![index],
          );
        },
      ),
    );
  }
}
